<?php
//连接数据库
$link=mysqli_connect('127.0.0.1','root','root','books-system');
mysqli_set_charset($link, 'utf8');
//搜索模块
$bookname = isset($_GET['bookname']) ? $_GET['bookname'] : '';
$query = "select * from books where bookname='$bookname'";
$result1 = mysqli_query($link, $query);
$data1 = mysqli_fetch_assoc($result1);
//获取用户名，书籍借阅状态
session_start();
$username=$_SESSION['username'];
$status=$data1['status'];
//更新借阅记录，更新书籍状态
if ($data1['status']==1) {    
    $run = "UPDATE books SET status=0 WHERE bookname='$bookname';";
    $lend = "INSERT INTO lend (username,bookname,statu) VALUES ('$username','$bookname','$status'); ";
}elseif($data1['status']==0){
    $run = "UPDATE books SET status=1 WHERE bookname='$bookname';";
    $lend= "INSERT INTO lend (username,bookname,statu) VALUES ('$username','$bookname','$status'); ";
};
//返回结果
$result = mysqli_query($link, $run);
$result2 = mysqli_query($link, $lend);
if ($result2) {
    echo '<script>alert("成功，点确定按钮后返回！");</script>';
    header("refresh:0;url=books.php");
} else {
    echo '<script>alert("失败，点确定按钮后返回！");</script>';
    header("refresh:0;url=books.php");
}
?>